package com.ruoyi.mobile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.mobile.domain.ShMarketEntity;
import com.ruoyi.mobile.domain.ShProductEntity;
import com.ruoyi.mobile.domain.ShProductPictureEntity;
import com.ruoyi.mobile.mapper.IShMarketMapper;
import com.ruoyi.mobile.mapper.IShProductMapper;
import com.ruoyi.mobile.mapper.IShProductPictureMapper;
import com.ruoyi.mobile.utils.NftResponse;
import com.ruoyi.mobile.utils.Paginator;
import com.ruoyi.mobile.utils.QueryUtil;
import com.ruoyi.mobile.utils.ResponseConstants;
import com.ruoyi.system.api.req.ShMarketReq;
import com.ruoyi.system.api.resp.ShMarketResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.service.IShMarketService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分销信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@Service
public class ShMarketServiceImpl extends ServiceImpl<IShMarketMapper, ShMarketEntity> implements IShMarketService {
    @Autowired
    private IShProductPictureMapper productPictureMapper;
    @Autowired
    private IShProductMapper productMapper;

    @Override
    public NftResponse<Paginator<ShMarketResp>> getMarketList(ShMarketReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        List<ShMarketResp> pageResult = null;
        if (null!=query.getProductType()&&query.getProductType()==0) {
            pageResult = this.baseMapper.getMarketPayList1(query.getCreateBy(), query.getAppId());
        }else if (null!=query.getProductType()&&query.getProductType()==-1) {
            pageResult = this.baseMapper.getMarketPayList2(query.getCreateBy(), query.getAppId());
        }else if (null!=query.getProductType()&&query.getProductType()==-2) {
            pageResult = this.baseMapper.getMarketPayList3(query.getCreateBy(), query.getAppId());
        }else {
            pageResult = this.baseMapper.getMarketPayList(query.getCreateBy(), query.getProductType(), query.getAppId());
        }
        for (ShMarketResp resp:pageResult) {
            ShProductEntity product =  productMapper.selectById(resp.getProductId());
            QueryWrapper<ShProductPictureEntity> wrapperQueryPic = new QueryWrapper<>();
            wrapperQueryPic.eq(QueryUtil.camelToUnderline("productId"), resp.getProductId());
            wrapperQueryPic.eq(QueryUtil.camelToUnderline("isVideo"), 0);
            List<ShProductPictureEntity> listPic = productPictureMapper.selectList(wrapperQueryPic);
            List<String> pics = new ArrayList<>();
            for (ShProductPictureEntity pic:listPic) {
                pics.add(pic.getProductPic());
            }
            resp.setProductName(product.getProductTitle());
            resp.setProductDesc(product.getProductDesc());
            resp.setProductPics(pics);
        }
        Paginator<ShMarketResp> paginator = new Paginator<ShMarketResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        Integer start = (query.getPageNo()-1)*query.getPageSize();
        Integer end = query.getPageNo()*query.getPageSize();
        List<ShMarketResp> rtnList = pageResult.subList(start>pageResult.size()?pageResult.size():start,end>pageResult.size()?pageResult.size():end);
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageResult.size());
        paginator.setPageData(rtnList);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<Boolean> clearMarket(ShMarketReq query) {
        List<ShMarketResp> pageResult = null;
        if (null!=query.getProductType()&&query.getProductType()==0) {
            pageResult = this.baseMapper.getMarketPayList1(query.getCreateBy(), query.getAppId());
        }else if (null!=query.getProductType()&&query.getProductType()==-1) {
            pageResult = this.baseMapper.getMarketPayList2(query.getCreateBy(), query.getAppId());
        }else if (null!=query.getProductType()&&query.getProductType()==-2) {
            pageResult = this.baseMapper.getMarketPayList3(query.getCreateBy(), query.getAppId());
        }else {
            pageResult = this.baseMapper.getMarketPayList(query.getCreateBy(), query.getProductType(), query.getAppId());
        }
        if (null!=pageResult) {
            List<Long> idList = pageResult.stream().map(o->o.getId()).collect(Collectors.toList());;
            this.baseMapper.deleteBatchIds(idList);
        }
        return null;
    }
}